const addEdit = {
  props: {
      title: {
        type: String,
      },
  },
  inject:['initData'],
  data(){
      return {
          is_show: false,
          btn_loading: false,
          handle: '',
          method: '',
          api: '',
          info: {},
          defaultProps: {
              children: 'children',
              label: 'title'
          },
          reset_form: {},
      }
  },
  computed:{
    init_data(){
      return this.initData;
    }
  },
  created() {
      this.reset_form = JSON.parse(JSON.stringify(this.form));
  },
  methods:{
      init(type,row) { // 弹窗初始化
        this.type = type;
        this.info = row || {};
          if (this.type == 'add') {
            this.handle = '新增';
            this.method = 'post';
          } else {
            this.handle = '编辑';
            this.method = 'put'
            this.$util.resetForm(this.form, row);
          }
          this.is_show = true;
          this.initCallback(type, row);
      },
      initCallback(type, row){}, // 编辑修改，回显数据的补充
      resetDialog() {
          // this.$util.resetForm(this.form, this.reset_form);
          this.form = JSON.parse(JSON.stringify(this.reset_form));
          this.btn_loading = false;
          this.resetDialogCallback();
      },
      resetDialogCallback(){}, // 关闭弹窗后的回调
      setName(id, type){
        let type_name = type.indexOf('_list') == -1 ? type+'_list' : type;
        let list = this.init_data[type_name];
        if(id != null){
          let name;
          let arr = list.filter(item => item.id == id);
          if(arr.length == 1){
            name = arr[0].name;
          }else{
            name = '';
          }				
                return name;
        }else{
          return '';
        }            
      },
      submitAddEdit() { //添加修改的提交
        if (this.verifyForm()) {
          this.submitFront();
          this.btn_loading = true;
          this.$request(
            this.method,
            this.api,
            this.form,
            (data, msg) => {
              this.$message.success('提交成功！');
              this.$emit('confirm', this.form);
              this.is_show = false;
              this.btn_loading = false;
              this.submitCallback(data);
            },
            msg => {
              this.btn_loading = false
              console.log(msg)
            }
          )
        }
      },
      submitFront(){}, // 添加修改保存前的补充
      submitCallback(data){}, // 添加修改提交后的回调

  }
}
export default addEdit;